<?php
/**
 * Created by PhpStorm.
 * User: pf
 * Date: 2017/12/11
 * Time: 17:12
 */

namespace App\Repositories;

use App\Game;
use App\GameConfig;
use App\GamePrize;
use App\GamePrizeRecord;
use App\Http\Controllers\Admin\Member\Repository\Member;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Auth;

class GameRepository
{
    //返回抽奖数据
    public function reward($gameId)
    {
        $gameConfig = GameConfig::with('gamePrize')->where('game_id', $gameId)->get();
        $reward = $this->getReward($gameConfig);
        //处理奖品
        if ($reward->prize_id != '') {
            $this->prizeHandle($reward);
        }
        return $reward;
    }

    public function post()
    {
        for ($i = 0; $i <= 300; $i++) {
            $client = new Client();
            $response = $client->post('https://s1.eqxiu.com/eqs/scene/count', [
                'form_params' => [
                    'sceneId' => 105809771,
                    'elementId' => 9449698242,
                    'pageId' => 995341453
                ]
            ]);
            $token = json_decode((string)$response->getBody(), true);

            sleep(1);
        }
    }
    //写入中奖记录
    protected function writeRecord($user_id, $game_id, $prize_id)
    {
        $record = new GamePrizeRecord();
        $prize=GamePrize::find($prize_id);
        $record->user_id = $user_id;
        $record->game_id = $game_id;
        $record->prize_id = $prize->name;
        $record->save();
    }

    //执行奖品处理
    public function prizeHandle($reward)
    {
        $game_prize = $reward->gamePrize;
        $user = Auth::guard('member')->user();
        if ($game_prize->type == 0) {//赠送景区次数
            $user->increment('wallet_spots', $game_prize->value);
            $this->writeRecord($user->id, $reward->game_id, $reward->prize_id);
        }
    }

    //抽奖算法
    public function getReward($gameConfig)
    {
        foreach ($gameConfig as $k => $v) {
            $win[$k] = floor($v->chance);
        }
        $other = 100 - array_sum($win);
        $return = [];
        foreach ($gameConfig as $k => $v) {
            for ($i = 0; $i < $win[$k]; $i++) {
                $return[] = $v;
            }
            $i = 0;
        }
        for ($i = 0; $i < $other; $i++) {
            $return[] = [
                'color' => [1, 2, 3],
                'prize_id' => '',
            ];
        }

        shuffle($return);
        return $return[array_rand($return)];
    }

    public function game($game_id)
    {
        return Game::with(['gamePrize'])->findOrFail($game_id);
    }
    public function getRecord($game_id){
      return   GamePrizeRecord::with('member','gamePrize')
          ->where('game_id',$game_id)
          ->orderBy('id','desc')
          ->take(20)
          ->get();
    }
    protected function checkGameNum($user){
      $member= $user;
      if($member->game_num<=0){
        return false;
      }else{
        return true;
      }
    }
    public function decrementGameNum($user){

        if($this->checkGameNum($user)) {
           return  $user->decrement('game_num',1);
        }else{
            return false;
        }
    }
}